﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OneKeyUnistalliTunes
{
    public partial class Form1 : Form
    {
        protected delegate void Display();

        private AppleAppUnistaller unistaller = null;

        public Form1()
        {
            InitializeComponent();
            this.lbName.Text = string.Empty;
            this.lbCount.Text = string.Empty;


            this.unistaller = new AppleAppUnistaller();
            this.unistaller.UninstallProgress += new EventHandler<UnistallProgressArgs>(unistaller_UninstallProgress);
        }

        private void unistaller_UninstallProgress(object sender, UnistallProgressArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Display(delegate() { this.unistaller_UninstallProgress(sender, e); }));
            }
            else
            {
                this.tbLog.Text = e.Log;
                this.lbName.Text = string.Format("当前卸载: {0}", e.Name);
                this.lbCount.Text = string.Format("总共有 {0} 项需要卸载", e.Count);

                if (e.IsFinished)
                {
                    this.progressBar1.Maximum = this.progressBar1.Value = 1;
                    this.btnStart.Enabled = true;
                    MessageBox.Show(e.Log);
                    this.Close();
                }
                else
                {
                    this.progressBar1.Maximum = e.Count;
                    if (this.progressBar1.Value < this.progressBar1.Maximum && e.CurrentCount < this.progressBar1.Maximum)
                        this.progressBar1.Value = e.CurrentCount;
                    else
                        this.progressBar1.Value = this.progressBar1.Maximum;
                }
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.tbLog.Text = string.Empty;
            this.progressBar1.Value = 0;
            this.btnStart.Enabled = false;

            this.unistaller.StartUnistall();

        }

        private void btnDeliTunesFolder_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("你确定要删除iTunes文件夹及它下面的所有文件吗?", "一键删除iTunes",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                if (this.unistaller.DeleteiTunesFolder())
                {
                    MessageBox.Show("iTunes文件夹删除成功或者已不存在");
                    this.btnDeliTunesFolder.Enabled = false;
                }
                else
                {
                    MessageBox.Show("删除iTunes文件夹失败,请检查相应的权限或者路径");
                }
            }
        }
    }
}
